/*!
 * \file t__split_eq_string.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frconfig.h"
#include "conntrack.h"
#include "frct_comm.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

int __split_eq_string(char *p_in, char *p_key, char **pp_value);

static void __case1(void)
{
    int i_ret = 0;
    char *p_value = NULL;
    char *p_in = NULL;
    char *p_key = (char *)"time";

    p_in = (char *)frmalloc(strlen("time = 10") + 1);
    if(p_in == NULL)
        return;

    snprintf(p_in,strlen("time = 10") + 1,"%s","time = 10");

    i_ret =__split_eq_string(p_in, p_key, &p_value);

    CU_ASSERT_STRING_EQUAL(p_value,"10");
    CU_ASSERT_EQUAL(i_ret,APM_FRCONFIG_SUCC);

    frfree(p_in);
    frfree(p_value);

}

static void __case2(void)
{
    int i_ret = 0;
    char *p_value = NULL;
    char *p_in = NULL;
    char *p_key = (char *)"time";

    p_in = (char *)frmalloc(strlen("   time = 10") + 1);
    if(p_in == NULL)
        return;

    snprintf(p_in,strlen("   time = 10") + 1,"%s","   time = 10");

    i_ret =__split_eq_string(p_in, p_key, &p_value);

    CU_ASSERT_STRING_EQUAL(p_value,"10");
    CU_ASSERT_EQUAL(i_ret,APM_FRCONFIG_SUCC);

    frfree(p_in);
    frfree(p_value);

}

static void __case3(void)
{
    int i_ret = 0;
    char *p_value = NULL;
    char *p_in = NULL;
    char *p_key = (char *)"time";

    p_in = (char *)frmalloc(strlen("time = -10") + 1);
    if(p_in == NULL)
        return;

    snprintf(p_in,strlen("time = -10") + 1,"%s","time = -10");

    i_ret =__split_eq_string(p_in, p_key, &p_value);

    CU_ASSERT_STRING_EQUAL(p_value,"-10");
    CU_ASSERT_EQUAL(i_ret,APM_FRCONFIG_SUCC);

    frfree(p_in);
    frfree(p_value);

}

static void __case4(void)
{
    int i_ret = 0;
    char *p_value = NULL;
    char *p_in = NULL;
    char *p_key = (char *)"time";

    p_in = (char *)frmalloc(strlen("time    =  10") + 1);
    if(p_in == NULL)
        return;

    snprintf(p_in,strlen("time    =  10") + 1,"%s","time    =  10");

    i_ret =__split_eq_string(p_in, p_key, &p_value);

    CU_ASSERT_STRING_EQUAL(p_value,"10");
    CU_ASSERT_EQUAL(i_ret,APM_FRCONFIG_SUCC);

    frfree(p_in);
    frfree(p_value);

}

static void __case5(void)
{
    int i_ret = 0;
    char *p_value = NULL;
    char *p_in = NULL;
    char *p_key = (char *)"time";

    p_in = (char *)frmalloc(strlen("time ") + 1);
    if(p_in == NULL)
        return;

    snprintf(p_in,strlen("time ") + 1,"%s","time ");

    i_ret =__split_eq_string(p_in, p_key, &p_value);

    CU_ASSERT_PTR_NULL(p_value);
    CU_ASSERT_EQUAL(i_ret,APM_FRCONFIG_NO_FIND);

    frfree(p_in);

}

static void __case6(void)
{
    int i_ret = 0;
    char *p_value = NULL;
    char *p_in = NULL;
    char *p_key = (char *)"time";

    p_in = (char *)frmalloc(strlen("time =") + 1);
    if(p_in == NULL)
        return;

    snprintf(p_in,strlen("time =") + 1,"%s","time =");

    i_ret =__split_eq_string(p_in, p_key, &p_value);

    CU_ASSERT_STRING_EQUAL(p_value,"\0");
    CU_ASSERT_EQUAL(i_ret,APM_FRCONFIG_SUCC);

    frfree(p_in);
    frfree(p_value);

}

static void __case7(void)
{
    int i_ret = 0;
    char *p_value = NULL;
    char *p_in = NULL;
    char *p_key = (char *)"time";

    p_in = (char *)frmalloc(strlen("time = ") + 1);
    snprintf(p_in,strlen("time = ") + 1,"%s","time = ");

    i_ret =__split_eq_string(p_in, p_key, &p_value);

    CU_ASSERT_STRING_EQUAL(p_value,"\0");
    CU_ASSERT_EQUAL(i_ret,APM_FRCONFIG_SUCC);

    frfree(p_in);
    frfree(p_value);

}
static void __case8(void)
{
    int i_ret = 0;
    char *p_value = NULL;
    char *p_in = NULL;
    char *p_key = (char *)"size";

    p_in = (char *)frmalloc(strlen("time = 10") + 1);
    if(p_in == NULL)
        return;

    snprintf(p_in,strlen("time = 10") + 1,"%s","time = 10");

    i_ret =__split_eq_string(p_in, p_key, &p_value);

    CU_ASSERT_PTR_NULL(p_value);
    CU_ASSERT_EQUAL(i_ret,APM_FRCONFIG_NO_FIND);

    frfree(p_in);

}

static void __case9(void)
{
    int i_ret = 0;
    char *p_value = NULL;
    char *p_in = NULL;
    char *p_key = NULL;

    p_in = (char *)frmalloc(strlen("time = 10") + 1);
    if(p_in == NULL)
        return;

    snprintf(p_in,strlen("time = 10") + 1,"%s","time = 10");

    i_ret =__split_eq_string(p_in, p_key, &p_value);

    CU_ASSERT_PTR_NULL(p_value);
    CU_ASSERT_EQUAL(i_ret,APM_FRCONFIG_NO_FIND);

    frfree(p_in);

}

static void __case10(void)
{
    int i_ret = 0;
    char *p_value = NULL;
    char *p_in = NULL;
    char *p_key = '\0';

    p_in = (char *)frmalloc(strlen("time = 10") + 1);
    if(p_in == NULL)
        return;

    snprintf(p_in,strlen("time = 10") + 1,"%s","time = 10");

    i_ret =__split_eq_string(p_in, p_key, &p_value);

    CU_ASSERT_PTR_NULL(p_value);
    CU_ASSERT_EQUAL(i_ret,APM_FRCONFIG_NO_FIND);

    frfree(p_in);

}
int add_suite___split_eq_string(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frcomm __split_eq_string()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);
    CU_add_test(pSuite, "case6", __case6);
    CU_add_test(pSuite, "case7", __case7);
    CU_add_test(pSuite, "case8", __case8);
    CU_add_test(pSuite, "case9", __case9);
    CU_add_test(pSuite, "case10", __case10);

    return FR_SUCC;
}
